//
//  UIButton+Add.m
//  AtCar
//
//  Created by 代佳宏 on 2020/11/25.
//  Copyright © 2020 车一转. All rights reserved.
//

#import "UIButton+Add.h"

@implementation UIButton (Add)

- (void)setBlock:(void(^)(UIButton*))block {
    objc_setAssociatedObject(self,@selector(block), block,OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
}

- (void(^)(UIButton*))block {
    return objc_getAssociatedObject(self,@selector(block));
}

- (void)addTapBlock:(void(^)(UIButton*))block {
    self.block= block;
    [self addTarget:self action:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
}

- (void)click:(UIButton*)btn {
    if(self.block) {
        self.block(btn);
    }
}

- (void)centerImageAndTitle:(float)space {
    // get the size of the elements here for readability
    CGSize imageSize = self.imageView.frame.size;
    NSString *title = self.currentTitle;
    UIFont *font = self.titleLabel.font;
    CGSize titleSize = [title sizeForFont:font size:CGSizeMake(self.frame.size.width, self.frame.size.height) mode:NSLineBreakByTruncatingMiddle];
    
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    [self setTitleEdgeInsets:UIEdgeInsetsMake(imageSize.height + space , -imageSize.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
    [self setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, titleSize.height + space, -titleSize.width)];//图片距离右边框距离减少图片的宽度，其它不边
}

- (void)centerImageAndTitle {
    const int DEFAULT_SPACING = 7.0f;
    [self centerImageAndTitle:DEFAULT_SPACING];
}

- (void)rightImageAndLeftTitle:(float)space {
    // get the size of the elements here for readability
    CGFloat halfSpace = space / 2;
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    
    // raise the image and push it right to center it
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0,
                                            titleSize.width,
                                            0.0,
                                            -titleSize.width - halfSpace);
    // lower the text and push it left to center it
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0,
                                            -imageSize.width - halfSpace,
                                            0.0,
                                            imageSize.width);
}

- (void)rightImageAndLeftTitle {
    const int DEFAULT_SPACING = 4.0f;
    [self rightImageAndLeftTitle:DEFAULT_SPACING];
}

/*1蓝色字体白色背景蓝色边框 2蓝色背景白色字体 3白色字体灰色子图 4黄色边框 黄色字体 5黄色背景 白色字体 6白色字体 灰色背景 7白色字体 红色背景*/
-(void)buttonImage:(NSInteger)index{
    switch (index) {
        case 2:{
            [self setTitleColor:[FSFormatTool colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
            [self setBackgroundImage:[[UIImage imageNamed:@"baseRectangle8"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) resizingMode:UIImageResizingModeStretch] forState:UIControlStateNormal];
        }
            break;
        case 1:{
            [self setTitleColor:[FSFormatTool colorWithHexString:@"#5AAEF2"] forState:UIControlStateNormal];
             [self setBackgroundImage:[[UIImage imageNamed:@"baseBox8"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) resizingMode:UIImageResizingModeStretch] forState:UIControlStateNormal];
        }
            break;
        case 3:{
            [self setTitleColor:[FSFormatTool colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
             [self setBackgroundImage:[[UIImage imageNamed:@"grayBox8"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) resizingMode:UIImageResizingModeStretch] forState:UIControlStateNormal];
        }
            break;
        case 4:{
            [self setTitleColor:[FSFormatTool colorWithHexString:@"#FB9F00"] forState:UIControlStateNormal];
             [self setBackgroundImage:[[UIImage imageNamed:@"baseYellowle"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) resizingMode:UIImageResizingModeStretch] forState:UIControlStateNormal];
        }
            break;
        case 5:{
            [self setTitleColor:[FSFormatTool colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
             [self setBackgroundImage:[[UIImage imageNamed:@"baseYellow"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) resizingMode:UIImageResizingModeStretch] forState:UIControlStateNormal];
        }
            break;
        case 6:{
            [self setTitleColor:[FSFormatTool colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
            [self setBackgroundImage:[[UIImage imageNamed:@"grayBox8"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) resizingMode:UIImageResizingModeStretch] forState:UIControlStateNormal];
            }
                break;
        case 7:{
            [self setTitleColor:[FSFormatTool colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
            [self setBackgroundImage:[[UIImage imageNamed:@"baseRed"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10) resizingMode:UIImageResizingModeStretch] forState:UIControlStateNormal];
            }
                break;
            
        default:
            break;
    }
}
@end
